<?php
if (!defined('BASEPATH'))
exit ('No direct script access allowed');

class Tenpay {

	var $CI;

	var $partner = "";
	var $key	 = "";


	public function __construct() {
		$this->CI = & get_instance();
		$this->CI->load->helper("tenpay/RequestHandler.class.php");
	}



	/**
	 *
	 *
	 * @name pay
	 * @date 2013-11-4
	 * @author eifel
	 * @param array 订单信息
	 * @return
	 * @version 1.0
	 * @description 财付通支付
	 */
	public function pay($order_info){
		/* 创建支付请求对象 */
		$reqHandler = new RequestHandler();
		$reqHandler->init();
		$reqHandler->setKey($this->key);
		$reqHandler->setGateUrl("https://gw.tenpay.com/gateway/pay.htm");


		$tenpay_config =$this->config->item("tenpay");
		$this->partner	= $tenpay_config['partner'];
		$this->key 		= $tenpay_config['key'];

		//----------------------------------------
		//设置支付参数
		//----------------------------------------
		$reqHandler->setParameter("total_fee", $order_info['total_fee']);  //总金额
		//用户ip
		$reqHandler->setParameter("spbill_create_ip", $_SERVER['REMOTE_ADDR']);//客户端IP
		$reqHandler->setParameter("return_url", $order_info['return_url']);//支付成功后返回
		$reqHandler->setParameter("partner", $this->partner);
		$reqHandler->setParameter("out_trade_no", $order_info['out_trade_no']);
		$reqHandler->setParameter("notify_url", $order_info['notify_url']);
		$reqHandler->setParameter("body", $order_info['body']);
		$reqHandler->setParameter("bank_type", "DEFAULT");  	  //银行类型，默认为财付通
		$reqHandler->setParameter("fee_type", "1");               //币种
		//系统可选参数
		$reqHandler->setParameter("sign_type", "MD5");  	 	  //签名方式，默认为MD5，可选RSA
		$reqHandler->setParameter("service_version", "1.0"); 	  //接口版本号
		$reqHandler->setParameter("input_charset", "UTF-8");   	  //字符集
		$reqHandler->setParameter("sign_key_index", "1");    	  //密钥序号

		//业务可选参数
		$reqHandler->setParameter("attach", "");             	  //附件数据，原样返回就可以了
		$reqHandler->setParameter("product_fee", "");        	  //商品费用
		$reqHandler->setParameter("transport_fee", "");      	  //物流费用
		$reqHandler->setParameter("time_start", date("YmdHis"));  //订单生成时间
		$reqHandler->setParameter("time_expire", "");             //订单失效时间

		$reqHandler->setParameter("buyer_id", "");                //买方财付通帐号
		$reqHandler->setParameter("goods_tag", "");               //商品标记


		//请求的URL
		$reqUrl = $reqHandler->getRequestURL();
		redirect($reqUrl);

	}


}
